package com.aviccxzx.smarthome.UserModule;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aviccxzx.smarthome.DeviceModule.GosDeviceModuleBaseActivity;
import com.aviccxzx.smarthome.MsgID;
import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.netutil.app.App;
import com.aviccxzx.smarthome.utils.ChangeNameDialogUtil;
import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiDeviceListener;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import static com.aviccxzx.smarthome.fragment.DeviceListFragment.addDeviceSucdev;


/**
 * Created by joey on 201610/25.
 */
public class AddDevSuccActivity extends GosDeviceModuleBaseActivity implements View.OnClickListener {
    TextView device_name_tv, title_right_text;
    public static AddDevSuccActivity addDevSuccInstance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.add_devsucc_lay1_v19);
        } else {
        }
        addDevSuccInstance = this;
        setView();
        setListener();
       /* if (addDeviceSucdev.getProductKey().equals(MsgID.PK_SMOKE)) {
            //如果是烟感的话 要区分类型
            addDeviceSucdev.setListener(deviceListener);
            addDeviceSucdev.getDeviceStatus(null);
        }*/
    }

    boolean getVersion = false;//判断是否获取到数据点信息
//    public static HashMap<String,String> newoldSmoke = new HashMap<>();
    public static List<String> newoldSmoke = new ArrayList<>();
    HashSet<String> smokeVer = new HashSet<>();
    /**
     * 获取上报的设备信息
     */
    private GizWifiDeviceListener deviceListener = new GizWifiDeviceListener() {
        @Override
        public void didReceiveData(GizWifiErrorCode result, GizWifiDevice device, ConcurrentHashMap<String, Object> dataMap, int sn) {
            super.didReceiveData(result, device, dataMap, sn);
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                if (dataMap.get("data") != null) {
                    ConcurrentHashMap<String, Object> map = (ConcurrentHashMap<String, Object>) dataMap.get("data");
                    byte[] hard = (byte[]) map.get("HardWareVersion");
                    String hardVi = new String(hard);
                    //区分新旧烟感
                    if (hardVi.endsWith("0")) {
                        //新烟感时
//                        smokeVer.add("");
//                        spf.edit().putStringSet("smokeVersion",smokeVer);
                    }
                    getVersion = true;
                }
            }
        }
    };

    private void setListener() {
        device_name_tv.setOnClickListener(this);
        findViewById(R.id.start_set_btn).setOnClickListener(this);
    }

    private void setView() {
        ((TextView) findViewById(R.id.title_tv)).setText("添加成功");
        title_right_text = (TextView) findViewById(R.id.title_right_text);
        title_right_text.setVisibility(View.VISIBLE);
        title_right_text.setText("确定");
        title_right_text.setOnClickListener(this);
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.set_if_rl).setOnClickListener(this);
        device_name_tv = (TextView) findViewById(R.id.device_name_tv);
    }

    String deviceName = "";

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.set_if_rl:
                //修改终端名称
                new ChangeNameDialogUtil(AddDevSuccActivity.this, "修改名称", new ChangeNameDialogUtil.SureInterfance() {
                    @Override
                    public void sureTodo(final String name) {
                        //确定修改 处理方案
                        deviceName = name;
                        if (addDeviceSucdev != null) {
                            if (addDeviceSucdev.isBind()) {
                                //设备绑定成功后才能修改
                                addDeviceSucdev.setCustomInfo("", name);
                                addDeviceSucdev.setListener(getGizWifiDeviceListener());
                                progressDialog.show();
                            } else {
                                //重新绑定
                                String uid = spf.getString("Uid", "");
                                String token = spf.getString("Token", "");
                                String type = addDeviceSucdev.getProductKey();
                                String ps = "";
                                if (type.equals(MsgID.PK_DOOR)) {
                                    ps = MsgID.PS_DOOR;
                                } else if (type.equals(MsgID.PK_WATER)) {
                                    ps = MsgID.PS_WATER;
                                } else if (type.equals(MsgID.PK_GAS)) {
                                    ps = MsgID.PS_GAS;
                                } else if (type.equals(MsgID.PK_SMOKE)) {
                                    ps = MsgID.PS_SMOKE;
                                } else if (type.equals(MsgID.PK_ALARM)) {
                                    ps = MsgID.PS_ALARM;
                                }
                                GizWifiSDK.sharedInstance().bindRemoteDevice(uid, token, addDeviceSucdev.getMacAddress(), addDeviceSucdev.getProductKey(), ps);
                                GizWifiSDK.sharedInstance().setListener(new GizWifiSDKListener() {
                                    @Override
                                    public void didBindDevice(GizWifiErrorCode result, String did) {
                                        if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                                            // 绑定成功
                                            //设备绑定成功后才能修改
                                            addDeviceSucdev.setCustomInfo("", name);
                                            addDeviceSucdev.setListener(getGizWifiDeviceListener());
                                            progressDialog.show();
                                        } else {
                                            // 绑定失败
                                            App.getApp().showToast("修改失败");
                                        }

                                    }
                                });
                            }
                        } else {
                            App.getApp().showToast("修改失败");
                        }
                    }
                }).showSelfDialog();
                break;
            case R.id.start_set_btn:
                //开始设置智能推荐场景
                Intent intent = new Intent(this, RecommendSetIfAct.class);
                startActivity(intent);
                break;
            case R.id.title_right_text:
                this.finish();
                break;
        }
    }

    /**
     * 修改名字回调
     *
     * @param result 错误码
     * @param device 当前设备
     */
    @Override
    public void didSetCustomInfo(GizWifiErrorCode result, GizWifiDevice device) {
        super.didSetCustomInfo(result, device);
        progressDialog.dismiss();
        if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
            // 修改成功
            App.getApp().showToast("修改成功");
            device_name_tv.setText(deviceName);
            //                finish();
        } else {
            //修改失败
            App.getApp().showToast("修改失败");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        addDevSuccInstance = null;
    }
}
